package com.tcm.controller.backend;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.dto.param.RoleTeacherUpdateParam;
import com.tcm.entity.resp.PageR;
import com.tcm.entity.resp.R;
import com.tcm.entity.vo.RoleSelectResult;
import com.tcm.entity.vo.RoleTeacherListResult;
import com.tcm.service.RoleService;
import com.tcm.utils.AccessTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 角色管理控制器
 * @author Guqier
 * @version 1.0
 * @date 2023/7/9 22:09
 */
@Slf4j
@RestController
@RequestMapping("/pc/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 获取角色列表
     * @return 角色列表响应结果
     */
    @RequestMapping(method = RequestMethod.GET)
    public R getRoleList(){
        List<RoleSelectResult> roleList = roleService.getRoleList();
        return R.success(roleList);
    }

    /**
     * 分页获取教师角色信息列表
     * @param page 当前页码
     * @param pageSize 页面大小
     * @param name 查询姓名，可选
     * @param account 查询账号，可选
     * @return 响应结果
     */
    @RequestMapping(value = "/teacher", method = RequestMethod.GET)
    public R getTeacherRoleListByPage(Integer page, Integer pageSize,
                                      String name, String account){
        if (page == null || pageSize == null){
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        PageR<RoleTeacherListResult> teacherRoleListByPage = roleService.getTeacherRoleListByPage(page, pageSize, name, account);
        return R.success(teacherRoleListByPage);
    }

    /**
     * 修改教师角色信息
     * @param param 参数封装对象
     * @param request http请求对象
     * @return 响应对象
     */
    @RequestMapping(value = "/teacher", method = RequestMethod.PUT)
    public R modifyTeacherRole(@RequestBody @Valid RoleTeacherUpdateParam param, HttpServletRequest request){
        boolean res = roleService.modifyTeacherRole(param, AccessTokenUtil.getUserId(request));
        return res ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

}
